﻿using aier.business.logic;
using aier.dataaccess.model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;

namespace aier.wx.api.selfhost.Controllers
{
    //[Authorize]
    [RoutePrefix("api/Competitor")]
   public class CompetitorController:ApiController
    {
        public CompetitorBll competitorBll { get; set; }

        public CommonBll commonBll { get; set; }

        public ApiTool apiTool { get; set; }


        [HttpGet]
        [Authorize]
        [ActionName("QueryCompetitorById")]
        public IHttpActionResult QueryCompetitorById(Guid competitorId)
        {
            var competitorView = competitorBll.QueryCompetitor(competitorId);
            return Ok(competitorView);
        }


        /// <summary>
        /// 根据不同条件查询医疗机构-竞争对手,只要属性不为空就视为查询条件
        /// 查询条件为：医疗机构ID/省份ID/市ID/区ID/社区ID/名称
        /// 原程序 LocationQueryServiceImpl/queryCompetitor
        /// </summary>
        /// <param name="competitorView">医疗机构对象</param>
        /// <returns></returns>
        [HttpPost]
        [ActionName("QueryCompetitor")]
        public IHttpActionResult QueryCompetitor(CompetitorView competitorView)
        {
            List<CompetitorView> competitorViews = competitorBll.QueryCompetitor(competitorView);
            return Ok<List<CompetitorView>>(competitorViews);
        }
        /// <summary>
        /// 根据不同条件查询医疗机构-竞争对手,只要属性不为空就视为查询条件
        /// 查询条件为：医疗机构ID/省份ID/市ID/区ID/名称
        /// 原程序 LocationQueryServiceImpl/queryCompetitorCount
        /// </summary>
        /// <param name="competitorView">医疗机构对象</param>
        /// <returns></returns>
        [HttpPost]
        [ActionName("QueryCompetitorCount")]
        public IHttpActionResult QueryCompetitorCount(CompetitorView competitorView)
        {
            int count = competitorBll.QueryCompetitorCount(competitorView);
            return Ok<int>(count);
        }

        /// <summary>
        /// 修改医疗机构
        /// </summary>
        /// <param name="competitorInfo"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionName("SaveCompetitor")]
        public IHttpActionResult SaveCompetitor(CompetitorInfo competitorInfo)
        {
            UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
            return Ok<ResResult>(competitorBll.UpdateCompetitor(competitorInfo, userInfo.userid));         
        }

        /// <summary>
        /// 新增医疗机构
        /// </summary>
        /// <param name="competitorInfo"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionName("AddCompetitor")]
        public IHttpActionResult AddCompetitor(CompetitorView competitorInfo)
        {
            UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
            return Ok<ResResult>(commonBll.SaveCompetitor(competitorInfo, userInfo.userid));
        }

    }
}
